
//
//  KZExperienceHeaderView.m
//  xiaorizi
//
//  Created by kun on 15/10/27.
//  Copyright © 2015年 origin. All rights reserved.
//



#import "KZExperienceHeaderView.h"
#import "experienceHeaderModel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"


#define SCREEN_W     [UIScreen mainScreen].bounds.size.width
#define HEADERVIEW_H 150

@interface KZExperienceHeaderView ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic)  NSMutableArray  *imgArrayM;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@end






@implementation KZExperienceHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+(KZExperienceHeaderView *)experienceTableHeaderView
{
    NSString *className = NSStringFromClass([self class]);
    return [[[NSBundle mainBundle]loadNibNamed:className owner:self options:nil]lastObject];
}



-(void)awakeFromNib
{
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    self.pageControl.currentPage = 0;
    
    self.imgArrayM = [NSMutableArray array];

}


-(void)setDataArray:(NSArray *)dataArray
{
    //初始化控件
    //NSLog(@"--->%@",dataArray);

    
    //1 需要添加清除之前的图片数据 强引用
    for(UIImageView *img in _imgArrayM)
    {
        [img removeFromSuperview];
    }
    [_imgArrayM removeAllObjects];
    
    

    for(int i = 0; i < dataArray.count ; i ++)
    {
        experienceHeaderModel *model = dataArray[i];
        UIImageView *imageV = [[UIImageView alloc]init];
        [imageV sd_setImageWithURL:[NSURL URLWithString:model.adurl]];
        [self.scrollView addSubview:imageV];
        imageV.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickedHeaderImg:)];
        [imageV addGestureRecognizer:tap];
        [self.scrollView addSubview:imageV];
        [_imgArrayM addObject:imageV];

        
        imageV.userInteractionEnabled  = YES;
    }
    
    //设置约束
    
    for(int i = 0; i < _imgArrayM.count; i ++)
    {
        UIImageView * imageView = _imgArrayM[i];
        UIImageView * lastView = i==0?nil:_imgArrayM[i-1];
        
        if (!lastView) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.scrollView.mas_top);
                make.left.equalTo(self.scrollView.mas_left);
                make.width.equalTo(self.scrollView.mas_width);
                make.height.equalTo(self.scrollView.mas_height);
            }];
        }
        else {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lastView.mas_top);
                make.left.equalTo(lastView.mas_right);
                make.width.equalTo(self.scrollView.mas_width);
                make.height.equalTo(self.scrollView.mas_height);
            }];
        }
    }
    
    //设置scroollViewcontentSize
    self.scrollView.contentSize = CGSizeMake(SCREEN_W * _imgArrayM.count,0);
    self.pageControl.numberOfPages = _imgArrayM.count;
    
    
    [self updateConstraintsIfNeeded];
}


-(void)clickedHeaderImg:(UITapGestureRecognizer *)tap
{
    //
    
    if(self.clickedExperienceHeaderBlock)
    {
        self.clickedExperienceHeaderBlock(self,_dataArray[tap.view.tag]);
    }
}



#pragma mark 滑动切换page

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = scrollView.contentOffset.x/SCREEN_W;
    //if(scrollView.contentOffset.x/)
}



@end
